package com.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class EmailService {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
//    @Value("${spring.mail.username:默认值}")//在application.properties中没有找到值时使用默认值
    private String from;

    public EmailService() {
    }

    public void sendEmail(String to, String subject, String content) throws MailException {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); //邮件发送者
        message.setTo(to); //邮件接受者
        message.setSubject(subject); //主题
        message.setText(content); //内容
        mailSender.send(message);
    }
//    @Scheduled(cron="0 15 10 15 * ?")//每月15号10点15分发送
    public void timgSendEmail(){
        String from = "1126813083@qq.com";
        String to = "523108285@qq.com";
        String subject = "定时邮件";
        String content = new Date().toString();
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); //邮件发送者
        message.setTo(to); //邮件接受者
        message.setSubject(subject); //主题
        message.setText(content); //内容
        mailSender.send(message);
    }
}
